package cn.org.qary.controller.rking.front;

import cn.org.qary.entity.AddressInfo;
import cn.org.qary.service.trait.rking.AddressInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Controller
@RequestMapping("addressInfo")
public class AddressInfoController {

    @Autowired
    AddressInfoService addressInfoService;

    @RequestMapping("find")
    public String findAll(Integer id,Model model){
        Example condition = new Example(AddressInfo.class);
        Example.Criteria criteria = condition.createCriteria();
        criteria.andEqualTo("userId",id);
        List<AddressInfo> addressInfoList = addressInfoService.selectByCondition(condition);
        model.addAttribute("addressInfoList",addressInfoList);
        return "myaddress";
    }

    @RequestMapping("insert")
    public String insertAddressInfo(AddressInfo addressInfo){
        addressInfoService.insert(addressInfo);
        return "redirect:find?id="+addressInfo.getUserId();
    }

    @RequestMapping("addmyaddress")
    @ResponseBody
    public AddressInfo addMyaddress(Integer id){
        AddressInfo info = addressInfoService.selectById(id);
        return info;
    }

    @RequestMapping("update")
    public String updateAddressInfo(AddressInfo addressInfo){
        addressInfoService.update(addressInfo);
        return "redirect:find?id="+addressInfo.getUserId();
    }

    @RequestMapping("delete")
    public String deleteAddressInfo(Integer id,Integer userId){
        addressInfoService.delete(id);
        return "redirect:find?id="+userId;
    }
}
